﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;

namespace CPPEI.WebApiTool.Writer
{
    class ConfigWriter
    {
        public static void Write(string filePath, DData data)
        {
            FileInfo fileInfo = new FileInfo(filePath);
            if (!fileInfo.Directory.Exists)
            {
                Console.WriteLine(string.Format("Create Directory{0}", fileInfo.Directory.FullName));
                fileInfo.Directory.Create();
            }

            XmlDocument xDoc = null;
            if (File.Exists(filePath))
            {
                xDoc = new XmlDocument();
                xDoc.Load(filePath);
            }
            else
            {
                xDoc = new XmlDocument();
            }

            XmlElement xWebApi = CPPEI.WebApi.Utils.XmlUtil.ElementFromDocument(xDoc, "WebApi");
            UpdateAuthMode(xWebApi, xDoc);

            WriteSettings(xWebApi);

            xDoc.Save(filePath);

            WriteApiRef(filePath, data);
            WriteConfigModules(filePath, data);
        }

        private static void WriteSettings(XmlNode xWebApi)
        {
            XmlElement xSettings = CPPEI.WebApi.Utils.XmlUtil.Element(xWebApi, "Settings");
            if (Program.s_settings.IsCamelCasePropertyName)
            {
                XmlElement xIsCamelCasePropertyName = CPPEI.WebApi.Utils.XmlUtil.Element(xSettings, "CamelCasePropertyName");
                xIsCamelCasePropertyName.InnerText = "true";
            }
            else
            {
                XmlElement xIsCamelCasePropertyName = CPPEI.WebApi.Utils.XmlUtil.Element(xSettings, "CamelCasePropertyName");
                xIsCamelCasePropertyName.InnerText = "false";
            }
        }

        private static void UpdateAuthMode(XmlNode xWebApi, XmlDocument xmlDoc)
        {
            XmlNode xAuthenticationMode = CPPEI.WebApi.Utils.XmlUtil.Element(xWebApi, "Authentication");
            if (Program.s_settings.AuthMode == Data.AuthMode.WebApi)
            {
                CPPEI.WebApi.Utils.XmlUtil.Attribute(xAuthenticationMode, "Mode", "WebApi");
            }
            else
            {
                CPPEI.WebApi.Utils.XmlUtil.Attribute(xAuthenticationMode, "Mode", "ASPXFORM");
            }
        }

        private static void WriteApiRef(string filePath, DData data)
        {

            //WebApiRefBuilder builder = new WebApiRefBuilder(data.AssemblyList);

            //builder.Build(filePath);

            //Console.WriteLine(string.Format("Create file{0}", filePath));
        }

        private static void WriteConfigModules(string filePath, DData data)
        {
            //WebInitiatorBuilder builder = new WebInitiatorBuilder(data.InitiatorList);

            //builder.Build(filePath);

            //Console.WriteLine(string.Format("Write intiator in config file:{0}{1}", System.Environment.NewLine, filePath));
        }
    }
}
